<?php
namespace Admin\Model;
use Think\Model;
class ShippingModel extends Model 
{
	protected $insertFields = array('shipping_code','shipping_name','shipping_desc','insure','support_cod','enabled','config_option','shipping_order');
	protected $updateFields = array('shipping_id','shipping_code','shipping_name','shipping_desc','insure','support_cod','enabled','config_option','shipping_order');
	protected $_validate = array(
		array('shipping_code', '1,20', '配送方式的字符串代号的值最长不能超过 20 个字符！', 2, 'length', 3),
		array('shipping_name', '1,120', '配送方式的名称的值最长不能超过 120 个字符！', 2, 'length', 3),
		array('shipping_desc', '1,120', '配送方式的描述的值最长不能超过 120 个字符！', 2, 'length', 3),
		array('insure', '1,10', '报价费用,单位元,或者是百分数,该值直接输出为报价的值最长不能超过 10 个字符！', 2, 'length', 3),
		array('support_cod', 'number', '是否支持货到付款,1,支持,0,不支持必须是一个整数！', 2, 'regex', 3),
		array('enabled', 'number', '配送方式是否被禁用,1,可用,0,不可用必须是一个整数！', 2, 'regex', 3),
		array('shipping_order', 'number', '排序必须是一个整数！', 2, 'regex', 3),
	);
	public function search($pageSize = 20)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($shipping_code = I('get.shipping_code'))
			$where['shipping_code'] = array('like', "%$shipping_code%");
		if($shipping_name = I('get.shipping_name'))
			$where['shipping_name'] = array('like', "%$shipping_name%");
		if($shipping_desc = I('get.shipping_desc'))
			$where['shipping_desc'] = array('like', "%$shipping_desc%");
		if($insure = I('get.insure'))
			$where['insure'] = array('like', "%$insure%");
		if($support_cod = I('get.support_cod'))
			$where['support_cod'] = array('eq', $support_cod);
		if($enabled = I('get.enabled'))
			$where['enabled'] = array('eq', $enabled);
		if($config_option = I('get.config_option'))
			$where['config_option'] = array('eq', $config_option);
		if($shipping_order = I('get.shipping_order'))
			$where['shipping_order'] = array('eq', $shipping_order);
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.shipping_id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
	}
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['shipping_id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
	}
	/************************************ 其他方法 ********************************************/
}